package logParser;

import com.opencsv.CSVReader;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @projectName: endurance_test
 * @className: LogParser
 * @description:
 * @author: JLONG
 * @date: 2024/10/25 10:14
 * @version: 1.0
 */
public class LogParser {

    public static void main(String[] args) {
        String logFilePath = "/Users/jlong/IdeaProjects/Endurance Test_panchip_3.1.6_pvt_NORMAL_noPIN_chan_9_-_24-10-23_093034/Endurance Test_panchip_3.1.6_pvt_chan_9_-_.csv";
        LogParser.run(logFilePath, true);
    }

    public static void run(String inputFileName, Boolean groupByID) {
        run(new File(inputFileName), groupByID);
    }

    public static void run(File inputFile, Boolean groupByID) {
        if (groupByID) {
            Map<String, LabelTask> labelDataMap = new HashMap<>();
            try {
                CSVReader reader = new CSVReader(new FileReader(inputFile));
                List<String[]> records = reader.readAll();
                for (String[] record : records) {
                    if (record[1].equals("TASK_FINISHED_EVENT") && !record[6].equals("LabelID")) {
                        String labelID = record[6];
                        if (!labelDataMap.containsKey(labelID)) {
                            labelDataMap.put(labelID, new LabelTask(labelID));
                            analyse(labelDataMap.get(labelID), record);
                        } else {
                            analyse(labelDataMap.get(labelID), record);
                        }
                    }
                }
                reader.close();

                for (String labelID : labelDataMap.keySet()) {
                    labelDataMap.get(labelID).printInfo();
                }

            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        } else {
            LabelTask labelData = new LabelTask("Total_Counter");
            try {
                CSVReader reader = new CSVReader(new FileReader(inputFile));
                List<String[]> records = reader.readAll();
                for (String[] record : records) {
                    if (record[1].equals("TASK_FINISHED_EVENT") && !record[6].equals("LabelID")) {
                        analyse(labelData, record);
                    }
                }
                reader.close();
                labelData.printInfo();


            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }

    private static void analyse(LabelTask labelTask, String[] record) {
        switch (record[5]) {
            case "IMAGE":
                updateTaskRate(labelTask.getTaskDataList().get(0), record[9]);
                break;
            case "FLASHING":
                updateTaskRate(labelTask.getTaskDataList().get(1), record[9]);
                break;
            case "QUERY_SENSOR":
                switch (record[17]) {
                    case "MCU_SUPPLY_VOLTAGE_SENSOR":
                        updateTaskRate(labelTask.getTaskDataList().get(2), record[9]);
                        updateTaskRate(labelTask.getTaskDataList().get(4), record[9]);
                        break;
                    case "BATTERY_RESISTANCE_SENSOR":
                        updateTaskRate(labelTask.getTaskDataList().get(3), record[9]);
                        break;
                }
                break;
            case "FORCE_CHANNEL_SCAN":
                updateTaskRate(labelTask.getTaskDataList().get(5), record[9]);
                break;
            case "QUERY_COUNTER":
                switch (record[17]) {
                    case "TOTAL_DISPLAY_UPDATES":
                        updateTaskRate(labelTask.getTaskDataList().get(6), record[9]);
                        break;
                    case "BROWNOUT_RESETS":
                        updateTaskRate(labelTask.getTaskDataList().get(7), record[9]);
                        break;
                    case "SCANNED_CHANNELS_LSB":
                        updateTaskRate(labelTask.getTaskDataList().get(8), record[9]);
                        break;
                    case "RESYNCS_MSB":
                        updateTaskRate(labelTask.getTaskDataList().get(9), record[9]);
                        break;
                }

        }
    }

    private static void updateTaskRate(TaskData labelTaskData, String taskStatus) {
        if (taskStatus.equals("SUCCESSFUL")) {
            labelTaskData.addSuccessCounter();
        } else if (taskStatus.equals("ERROR")) {
            labelTaskData.addFailedCounter();
        }
    }
}
